import comm from './core';
import { APIORIGIN } from './meta';

function transformMediaData (data) {
  const {
    code,
    displayName,
    subtitle,
    bigPic,
    smallPic,
    tags,
    score,
    description,
    programType,
    videoType,
    stat
  } = data;

  const bigImage = bigPic || smallPic;
  return {
    sid: code,
    title: displayName,
    subtitle,
    bigImage: comm.formatZoomPic(bigImage),
    smallImage: comm.formatZoomPic(smallPic) || comm.formatZoomPic(bigPic),
    score: (+score).toFixed(1),
    scoreSeries: comm.dealScore(score),
    intro: description,
    tags: (tags && tags.length > 0) ? tags.split('-') : [],
    programType,
    videoType,
    playCount: comm.dealNumber(stat.playCount) || 0
  };
}

function get () {
  const params = comm.requestParams();

  const {
    code
  } = params;

  const path = `dynaprogram/findByCode`;
  const query = `code=${code}`;
  return comm.request({ origin: APIORIGIN, path, query }).then(({ data }) => {
    if (data) {
      return transformMediaData(data) || {};
    }
  });
}

export default {
  get
};
